import myAxios from "../plugins/MyAxios.ts";
// @ts-ignore
import qs from "qs";

/**
 * 用户主页推荐api
 * @param pageIndex
 * @param pageSize
 */
export const getUserRecommend = (pageIndex: number = 1, pageSize: number = 8) =>
    myAxios.get('/user/recommend', {
        params: {
            pageIndex,
            pageSize,
        },
        // 参数序列化
        paramsSerializer: params => {
            return qs.stringify(params, {indices: false})
        }
    });

/**
 * 用户主页心动模式标签匹配
 * @param num
 */
export const getUserMatches = (num: number) =>
    myAxios.get('/user/matches', {
        params: {
            num,
        },
    });

/**
 * 根据标签搜索用户信息
 * @param tags
 */
export const getUserSearchTags = (tags: string) => myAxios.get('/user/search/tags', {
    params: {
        tagNameList: tags,
        pageIndex: 1,
        pageSize: 8
    },
    paramsSerializer: params => {
        return qs.stringify(params, {indices: false})
    }
})

/**
 * 获取当前登录用户信息
 */
export const getLoginUser = () => myAxios.get('/user/current');

/**
 * 修改用户信息（包括头像）
 * @param userId 用户ID
 * @param userData 用户数据对象，包含要更新的字段
 */
export const updateUserInfo = (userId: number, editKey: string, editValue: any) => {
    // 动态构建参数：{ id: userId, 字段名: 字段值 }
    const data = {
        id: userId,
        [editKey]: editValue, // 关键：用方括号解析动态字段名
    };
    return myAxios.post('/user/update', data);
};

/**
 * 修改用户头像
 * @param userId
 * @param avatarUrl
 */
export const updateUserAvatarUrl = (userId: number, avatarUrl: string) => {
    return updateUserInfo(userId, "avatarUrl", avatarUrl);
};

/**
 * 登录
 * @param userAccount
 * @param userPassword
 */
export const userLogin = (userAccount: string, userPassword: string) => myAxios.post('/user/login', {
    userAccount: userAccount,
    userPassword: userPassword,
});

/**
 * 退出登录
 */
export const userLogout = () => myAxios.post('/user/logout');

/**
 * 用户注册
 */
export const userRegister = (userAccount: string, userPassword: string, checkPassword: string, planetCode: string) => myAxios.post('/user/register', {
    userAccount: userAccount,
    userPassword: userPassword,
    checkPassword: checkPassword,
    planetCode: planetCode
});

// 新增一个专门用于修改密码的API
export const updateUserPassword = (userId: number, oldPassword: string, newPassword: string, checkPassword: string) => {
    return myAxios.post('/user/update', {
        id: userId,
        userPassword: oldPassword,
        newPassword: newPassword,
        checkPassword: checkPassword,
    });
};

/**
 * 根据id获取用户信息
 * @param id
 */
export const getUserById = (id: number) => myAxios.get(`/user/list/${id}`);

